#ifndef BASE_VOIP_ENGINE_RELAY_H_
#define BASE_VOIP_ENGINE_RELAY_H_
#include "ct_noncopyable.h"
#include "ct_udp_socket.h"
#include "ct_udp_msg_buf.h"
#include "ctRWLock.h"
#include "ct_mutex.h"
#include "ctSingleton.h"
#include "ct_voip_lost_checker_mgr.h"
#include "ct_voip_msg_cache_mgr.h"

typedef __gnu_cxx::hash_map<DWORD, struct sockaddr_in> ClientAddrMap;
typedef ClientAddrMap::iterator CAM_IT;

class VOIPEngineRelayServer:
        public ctThread
{
        public:
                VOIPEngineRelayServer();
                ~VOIPEngineRelayServer(){}
                bool init(const WORD &port);
                bool delClient(const WORD &clientid);
                bool addClient(const WORD &clientid, const std::string &ip, const WORD &port);
        private:
                void run(); //线程主回调函数
                void recvData();
                void sendData();
                void sendDataRTP(const void *data, const DWORD len);
                void sendDataRTCP(const void *data, const DWORD len);
                bool putClientAddr(const WORD &clientid, struct sockaddr_in &, const BYTE &type);/*type:0-rtp, 1-rtcp*/
                void cacheSendPacket(const BYTE* data, DWORD length);
                void checkPacketLost(BYTE* data, DWORD length, struct sockaddr_in &clientaddr);

        private:
                //rtp data
                WORD port_rtp_;
                ctUDPSocket socket_rtp_;
                fd_set readfds;

                t_BufferCmdQueue snd_queue_rtp_;
                ctMutex mutex_srtp_;

                ClientAddrMap client_addr_map_rtp_;
                ctRWLock cam_lock_rtp_;

                //rtcp data
                WORD port_rtcp_;
                ctUDPSocket socket_rtcp_;

                t_BufferCmdQueue snd_queue_rtcp_;
                ctMutex mutex_srtcp_;

                ClientAddrMap client_addr_map_rtcp_;
                ctRWLock cam_lock_rtcp_;

                BYTE recv_buffer_[10*1024];

                // packet lost checker for: client->server packets.
                ctVOIPLostCheckerMgr    audio_lost_checker_;
                ctVOIPLostCheckerMgr    video_lost_checker_;

                //cache for: server->relayserver packets.
                ctVOIPMsgCacheMgr       audio_send_cache_;
                ctVOIPMsgCacheMgr       video_send_cache_;

                Timer                   one_min_timer_;
};

#endif //BASE_VOIP_ENGINE_RELAY_H_
